{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "926718e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "import matplotlib.pyplot as plt\n",
    "from sympy import symbols, cos, sin\n",
    "from sympy.plotting import plot_parametric\n",
    "from math import sin,cos\n",
    "import io\n",
    "import cv2\n",
    "import pykalman\n",
    "from scipy import optimize\n",
    "import matplotlib as mpl\n",
    "from numpy import array, arange, abs as np_abs\n",
    "from numpy.fft import rfft, rfftfreq\n",
    "from math import sin, pi\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mpl\n",
    "import cmath\n",
    "import pandas as pd\n",
    "from scipy.io import wavfile"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81a1afdc",
   "metadata": {},
   "source": [
    "# Лабораторная работа 2.7\n",
    "Реализован метод фазового кодирования (phase coding), изложенный в работе W. Bender, D. Gruhl, N. Morimoto, A. Lu. Techniques for data hiding. IBM Systems Journal. Vol. 35, Issue: 3-4, 1996. doi: 10.1147/sj.353.0313\n",
    "https://pdfs.semanticscholar.org/8c82/c93dfc7d3672e58efd982a23791a8a419053.pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ab4606a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "binary_string =  \"11000011100010010000\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e3954b19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000000\n"
     ]
    }
   ],
   "source": [
    "Fs, data = wavfile.read('./tune1.wav')\n",
    "#data = data[0:20000]\n",
    "#print(Fs)\n",
    "#print(data)\n",
    "n = len(data)\n",
    "print(n)\n",
    "N = 100\n",
    "l = n//N"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "f8a20a82",
   "metadata": {},
   "outputs": [],
   "source": [
    "ph = []\n",
    "deltaph = []\n",
    "A = []\n",
    "ph.append(np.array([0]*l))\n",
    "A.append(np.array([0]*l))\n",
    "for i in range(N):\n",
    "    q = data[i * l:((i+1) * l)].copy()\n",
    "    ft = np.fft.fft(q)\n",
    "    phi = []\n",
    "    a = []\n",
    "    for x in ft:\n",
    "        phi.append(cmath.phase(x))\n",
    "        a.append(abs(x))\n",
    "    deltaph.append(np.array(phi) - np.array(ph[-1]))\n",
    "    A.append(a)\n",
    "    ph.append(phi)\n",
    "   # if i == 0 :\n",
    "   #     freqs = np.fft.fftfreq(len(ft), d=1/l)\n",
    "   #     plt.figure(figsize=(20, 10))\n",
    "   #     plt.plot(freqs, phi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "0445b4a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f89238d8a90>]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABIEAAAJJCAYAAAA9XSciAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAnuUlEQVR4nO3de7BlZ13n4e+PNAFLBNS0SiXBpMbomFEZmBYZqRrjgBouJs4MWkl5ASaSohR1RmYwCANUcGpgqAEFAxoBucgQIqKkMBACBIGRQJpb7iFNyKVz604691un0+/8cXaHQ3P6nN199jlr7/0+T1UXZ6+9eq9fV/WmVn+y1ruqtRYAAAAA5tsjhh4AAAAAgLUnAgEAAAB0QAQCAAAA6IAIBAAAANABEQgAAACgAyIQAAAAQAcGjUBV9c6q2lZVF4+x7xOr6ryq+kpVXVhVz16PGQEAAADmwdBXAr0rybFj7vvKJGe21p6c5IQkb12roQAAAADmzaARqLX2mSQ7Fm+rqn9RVR+rqi9V1Wer6l/u2T3JY0c/Py7JDes4KgAAAMBM2zD0AEs4PcmLW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7Ru/fMdrf94R55+843RndrvLkJF/I/p8nTfJcDKbVn2bhP1LvHr2e5DnUgZynwdg2DD0AVNUnkvzQEm+9IskfZ+FWsHXRWmtV1VbeE9bXct+T1tqHR/u8IguX879vPWcDYH5U1WOS/F2S/9Jau3O0bE+S9TlPci7GtKuq5ybZ1lr7UlUdM/A4sN9EIAbXWnvmUtur6ieTHJnka6MTkMOSfLmqnprk+iSHL9r9sNG265Mcs9f2T4+2H7bE/klyc1U9obV24+hS5W2j7fs6Bqy7fX1P9qiqFyR5bpJnjC6zT5b/O7zU9luzcDn/htF/ZVq8/57P2lpVG5I8brS/7wnzzt9xulFVj8xCAHpfa+1Do837e540yXMxmEZPT3JcVT07yaOTPDbJn2Wy51D7e54GY3M7GFOrtXZRa+0HWmtHtNaOyMIlj09prd2U5KwkvzV6MsXTktwxuoz4nCS/WFXfO1qE8BeTnDN6786qetroftrfSvLh0aHOSrLnyRXP32v7UseAqVJVx2bhkuTjWmv3LnrrrCQnjJ5KcWQWFuH8YpILkhw1esLEwVlYlPCsUTw6L8nzRr9/7+/Dnu/J85J8arT/vo4B82LJ78vAM8HEjc6P3pHkstbaGxe9tb/nSZM8F4Op01p7eWvtsNG/T07IwjnRr2dy51AHcp4GY3MlELPq7CTPzsICavcmeWGStNZ2VNVrs/B/nklyamttx+jn30nyriTflYU1U/asm/K6JGdW1UlJrknya8sdA6bQnyd5VJJzR1fNnd9ae3Fr7ZKqOjPJpVm4Tex3W2sPJUlVvSQLJ+oHJXlna+2S0Wf9UZIzqupPknwlC/8gyOh/31tVW5LsyMIJSZY7BsyD1tquZb4vME+enuQ3k1xUVV8dbfvj7Od50oTPxWCWTOwc6gDO02Bs9a27BgAAAACYV24HAwAAAOiACAQAAADQAREIAAAAoAMiEAAAAEAHRCAAAACADohAAAAAAB0QgQAAAAA68P8B7j/AwtReyg4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 1440x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ft = np.fft.fft(data)\n",
    "freqs = np.fft.fftfreq(len(ft), d=1/n)\n",
    "\n",
    "plt.figure(figsize=(20, 10))\n",
    "plt.plot(freqs, abs(ft))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "862f107f",
   "metadata": {},
   "outputs": [],
   "source": [
    "phidata = []\n",
    "for c in binary_string:\n",
    "    if c == '0':\n",
    "        phidata.append(math.pi/2)\n",
    "    else:\n",
    "        phidata.append(-math.pi/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "f652731a",
   "metadata": {},
   "outputs": [],
   "source": [
    "phans = []\n",
    "lm = l//2 - len(binary_string)\n",
    "e = deltaph[0].copy()\n",
    "for i in range(lm,lm+len(binary_string)):\n",
    "    e[i] +=  phidata[i - lm]\n",
    "for i in range(l//2,l//2+len(binary_string)):\n",
    "    e[i] +=  phidata[len(binary_string) - (i - l//2) - 1]\n",
    "phans.append(e)\n",
    "for i in range(1,len(deltaph)):\n",
    "    phans.append(np.array(phans[-1]) +  np.array(deltaph[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "565c6a28",
   "metadata": {},
   "outputs": [],
   "source": [
    "data2 = []\n",
    "for i in range(0,N):\n",
    "    cod_data = []\n",
    "    for j in range(0,len(A[i+1])):\n",
    "      #  print(A[i+1][j])\n",
    "      #  print(phans[i][j])\n",
    "        num = cmath.rect(A[i+1][j],phans[i][j])\n",
    "        cod_data.append(num)\n",
    "    cod_data = np.fft.ifft(cod_data)\n",
    "    for i in cod_data:\n",
    "        data2.append(i)\n",
    "#print(cod_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "607d5d4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABIEAAAJJCAYAAAA9XSciAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLj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5w/2n9/x4B5seT3ZeCZYOJG50fvSnJla+3Ni9460POkSZ6LwdRprb2qtXb46N9PTsjCOdGvZHLnUAdzngZjcyUQs+qcJM/LwgJq9yV5SZK01nZW1euz8H+eSXJqa23n6OffTPKeJP8iC2um7F035Q1Jzqqqk5Jcn+SXlzsGTKE/S/LYJOeNrpq7qLX2stba5VV1VpIrsnCb2G+11h5Okqp6eRZO1A9J8u7W2uWjz/qDJGdW1R8n+VIW/oUgo3++v6q2JtmZhROSLHcMmAettd3LfF9gnjwrya8lubSqvjza9oc5wPOkCZ+LwSyZ2DnUQZynwdjqn+8aAAAAAGBeuR0MAAAAoAMiEAAAAEAHRCAAAACADohAAAAAAB0QgQAAAAA6IAIBAAAAdEAEAgAAAOjA/wc7dbcHA7ZUKgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 1440x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ft = np.fft.fft(data2)\n",
    "freqs = np.fft.fftfreq(len(ft), d=1/n)\n",
    "\n",
    "plt.figure(figsize=(20, 10))\n",
    "plt.plot(freqs, abs(ft))\n",
    "data_ans = []\n",
    "for i in data2:\n",
    "    data_ans.append(np.int16(i.real))\n",
    "wavfile.write('./tune_filt1.wav', Fs, np.int16(data_ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e1a22abf",
   "metadata": {},
   "outputs": [],
   "source": [
    "N = n // l\n",
    "ph1 = []\n",
    "deltaph1 = []\n",
    "A1 = []\n",
    "ph1.append(np.array([0]*l))\n",
    "A1.append(np.array([0]*l))\n",
    "for i in range(N):\n",
    "    q = data2[i*l:((i+1)*l)].copy()\n",
    "    ft = np.fft.fft(q)\n",
    "    phi = []\n",
    "    a = []\n",
    "    for x in ft:\n",
    "        phi.append(cmath.phase(x))\n",
    "        a.append(abs(x))\n",
    "    deltaph1.append(np.array(phi) - np.array(ph1[-1]))\n",
    "    A1.append(a)\n",
    "    ph1.append(phi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "3f45c12d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def normalize(phi):\n",
    "    while phi > math.pi:\n",
    "        phi-= 2*math.pi\n",
    "    while phi < -math.pi:\n",
    "        phi+=2*math.pi\n",
    "    return phi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "2c07d358",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11000011100010010000\n",
      "[0.0000000e+00 0.0000000e+00 0.0000000e+00 ... 8.8817842e-16 4.4408921e-16\n",
      " 4.4408921e-16]\n",
      "[-1.5707963267948966, -1.5707963267948966, 1.5707963267948966, 1.5707963267948966, 1.5707963267948966, 1.5707963267948966, -1.5707963267948966, -1.5707963267948966, -1.5707963267948966, 1.5707963267948966, 1.5707963267948966, 1.5707963267948966, -1.5707963267948966, 1.5707963267948966, 1.5707963267948966, -1.5707963267948966, 1.5707963267948966, 1.5707963267948966, 1.5707963267948966, 1.5707963267948966]\n"
     ]
    }
   ],
   "source": [
    "mynewdata  = ph1[1] - deltaph[0] \n",
    "for i in range(len(mynewdata)):\n",
    "    mynewdata[i] = normalize(mynewdata[i])\n",
    "s = \"\"\n",
    "lm = l//2 - len(binary_string)\n",
    "for  x in range(lm,lm+len(binary_string)):\n",
    "    if mynewdata[x] < 0 :\n",
    "        s+=\"1\"\n",
    "    else:\n",
    "        s+=\"0\"\n",
    "print(s)\n",
    "print(mynewdata)\n",
    "print(phidata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ac92214",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47ee4258",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a9690b0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa46fd1a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
